﻿using System.Threading.Tasks;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices;
using PeiYue.Model;
using PeiYue.Model.Models;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Model.Dto.User;
using System.Linq.Expressions;
using System;
using PeiYue.Common.Helper;
using PeiYue.Api.Controllers.BASE;
using AutoMapper;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;

namespace PeiYue.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class RoleController : BaseController<Role>
    {
        readonly IRoleServices _roleServices;
        readonly IUser _user;
        readonly IMapper _mapper;


        public RoleController(IRoleServices roleServices, IMapper mapper, IUser user) : base(roleServices, user)
        {
            _roleServices = roleServices;
            this._mapper = mapper;
            _user = user;
        }

        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        // GET: api/User
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<PageModel<Role>>> Get([FromQuery] SearchRoleDto querymodel)
        {

            Expression<Func<Role, bool>> ex = t => true;

            if (!string.IsNullOrEmpty(querymodel.name))
                ex = ex.And(t => t.Name.Contains(querymodel.name));
            if (querymodel.status == null || querymodel.status == 0)
                ex = ex.And(t => t.Status != 3);
            else
                ex = ex.And(t => t.Status == querymodel.status);

            var data = await _roleServices.QueryPage(ex, querymodel.pageIndex, querymodel.pageSize, " OrderSort, Id desc ");

            return new MessageModel<PageModel<Role>>()
            { msg = "获取成功", success = data.dataCount >= 0, response = data };

        }



        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Save([FromBody] RoleDto model)
        {
            var dao = _mapper.Map<Role>(model);
            dao.update_user = _user.ID;
            dao.create_user = _user.ID;
            dao.update_time = DateTime.Now;
            var data = new MessageModel<string>();
            if (model.Id > 0)
            {
                data.success = await _roleServices.Update(dao, null, new List<string> { "create_time", "create_user" });
            }
            else
            {
                dao.create_user = _user.ID;
                dao.create_time = DateTime.Now;
                dao.Status = 1;
                dao.Id = await _roleServices.Add(dao);
                data.success = dao.Id > 0;
            }
            if (data.success)
            {
                data.msg = "保存成功";
                data.response = dao?.Id.ObjToString();
            }
            return data;
        }



        //// GET: api/User/5
        //[HttpGet("{id}")]
        //public string Get(string id)
        //{
        //    return "value";
        //}

        ///// <summary>
        ///// 添加角色
        ///// </summary>
        ///// <param name="role"></param>
        ///// <returns></returns>
        //// POST: api/User
        //[HttpPost]
        //public async Task<MessageModel<string>> Post([FromBody] Role role)
        //{
        //    var data = new MessageModel<string>();

        //    role.CreateId = _user.ID;
        //    role.CreateBy = _user.Name;

        //    var id = (await _roleServices.Add(role));
        //    data.success = id > 0;
        //    if (data.success)
        //    {
        //        data.response = id.ObjToString();
        //        data.msg = "添加成功";
        //    }

        //    return data;
        //}

        ///// <summary>
        ///// 更新角色
        ///// </summary>
        ///// <param name="role"></param>
        ///// <returns></returns>
        //// PUT: api/User/5
        //[HttpPut]
        //public async Task<MessageModel<string>> Put([FromBody] Role role)
        //{
        //    var data = new MessageModel<string>();
        //    if (role != null && role.Id > 0)
        //    {
        //        data.success = await _roleServices.Update(role);
        //        if (data.success)
        //        {
        //            data.msg = "更新成功";
        //            data.response = role?.Id.ObjToString();
        //        }
        //    }

        //    return data;
        //}

        ///// <summary>
        ///// 删除角色
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //// DELETE: api/ApiWithActions/5
        //[HttpDelete]
        //public async Task<MessageModel<string>> Delete(int id)
        //{
        //    var data = new MessageModel<string>();
        //    if (id > 0)
        //    {
        //        var userDetail = await _roleServices.QueryById(id);
        //        userDetail.IsDeleted = true;
        //        data.success = await _roleServices.Update(userDetail);
        //        if (data.success)
        //        {
        //            data.msg = "删除成功";
        //            data.response = userDetail?.Id.ObjToString();
        //        }
        //    }

        //    return data;
        //}
    }
}
